package com.example.a33mvvm.fragment_viewpage;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.widget.ImageView;

import com.example.a33mvvm.R;
import com.example.a33mvvm.adapter.CommonTabEntity;
import com.example.a33mvvm.adapter.ViewPager2Adapter;
import com.example.a33mvvm.home.BannerViewModel;
import com.example.a33mvvm.databinding.FragmentHomeBinding;
import com.example.a33mvvm.fragment_homeviewpage.FirstFragment;
import com.example.a33mvvm.fragment_homeviewpage.SecondFragment;
import com.example.a33mvvm.fragment_homeviewpage.ThirdFragment;
import com.example.base_mvvm.base.view.BaseFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends BaseFragment<FragmentHomeBinding, BannerViewModel>{
    private List<String> images = new ArrayList<>();
    private ArrayList<CustomTabEntity> tabs = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private ViewPager2Adapter viewPager2Adapter;
    @Override
    protected void initData() {
        tabs.clear();
        fragments.clear();
        tabs.add(new CommonTabEntity("听说"));
        tabs.add(new CommonTabEntity("午夜会有"));
        tabs.add(new CommonTabEntity("流星雨"));
        dataBinding.homeTab.setTabData(tabs);
        fragments.add(new FirstFragment());
        fragments.add(new SecondFragment());
        fragments.add(new ThirdFragment());
        viewPager2Adapter = new ViewPager2Adapter(requireActivity(),fragments);
        dataBinding.homeVp2.setAdapter(viewPager2Adapter);
        viewModel.banner();
        viewModel.banner_success.observe(this, bannerEntity -> {
            if(bannerEntity.getCode() == 200){
                for (int i = 0; i < bannerEntity.getData().size(); i++) {
                    images.add(bannerEntity.getData().get(i).getImagePath());
                }
                dataBinding.homeBanner.setImages(images);
                dataBinding.homeBanner.start();
            }else {
                Log.d("sss", "onChanged: "+bannerEntity.getMessage());
            }
        });
        viewModel.banner_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.d("sss", "onChanged: "+s);
            }
        });
    }

    @Override
    protected void initView() {

        dataBinding.homeVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.homeTab.setCurrentTab(position);
            }
        });

        dataBinding.homeTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.homeVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        dataBinding.homeBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                com.example.a33mvvm.utils.ImageLoader.getInstance().LoadImage(context,path.toString(),imageView);
            }
        });
    }

    @Override
    protected Class<BannerViewModel> getViewModel() {
        return BannerViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}